package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.nextfm.dao.ComentarioDao;
import br.nextfm.dao.MusicaDao;
import br.nextfm.model.Comentario;
import br.nextfm.model.Musica;

public class RemoveComentario extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		ComentarioDao dao = new ComentarioDao();
		MusicaDao musicaDao = new MusicaDao();
		
		Comentario comentario = dao.get(id);
		Musica musica = comentario.getMusica();
		
		musica.getComentarios().remove(comentario);
		
		System.out.println(musica.getNome());
		System.out.println(musica.getId());
		
		if (musicaDao.update(musica)) {
			dao.delete(id);
		}
		
		request.setAttribute("musica", musica);
		request.getRequestDispatcher("escutaMusicas.jsp").forward(request, response);
		
	}

}
